<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class BaseModel extends Eloquent {
    
    protected $TAKE_NUM = 60;
    
    protected $request =null;
    // 验证请求参数格式参数
    protected $connection = '';
    
    //所有的模型
    protected $models = array(
        'app_version'=> AppVersion::class,
        'attention'=>Attention::class,
        'book_record'=> BookRecord::class,
        'category'=> Category::class,
        'favorite'=>Favorite::class,
        'gift'=>Gift::class,
        'live'=>Live::class,
        'live_has_category'=>LiveHasCategory::class,
        'quick_msg'=>QuickMsg::class,
        'recommend'=> Recommend::class,
        'recomend_category'=> RecommendCategory::class,
        'resource'=>Resource::class,
        'short_msg'=>ShortMsg::class,
        'short_msg_record'=>ShortMsgRecord::class,
        'stop_words'=>Stopwords::class,
        'user'=> User::class,
        'view_history'=>ViewHistory::class,
    );
    
    
    // 验证请求参数格式参数
    public function __construct( $request = null) {
        $this->connection = env('MYSQL_CONNECTION','mysql');
        $this->request = $request;
    }
    
    // 版本号比较
	public function  compareVison($va,$vb){
		$arr_av = explode('.', $va);
		$arr_vb = explode('.', $vb);
		foreach ($arr_av as $key => $value) {
			if(intval($value) > intval($arr_vb[$key])){
				return 1;
			}else if(intval($value) < intval($arr_vb[$key])){
				return -1;
			}
		}
		return 0;
	}
    
    //获取随机的大写字符
    public function getRandChar($length){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $max = strlen($strPol)-1;

        for($i=0;$i<$length;$i++){
         $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }
        return $str;
    }
    
    //同步数据
    public function snycDBModel(){
        //通过table 变量模型
        $table = $this->request->input('table');
        if(isset($this->models[$table])){
            with(new $this->models[$table]($this->request))->SnycModelHandle();
        }
    }
    
    public function SnycModelHandle(){
        //通过table 变量模型
        return;
    }
    
	//重写模型自动维护时间格式
	protected function getDateFormat(){
        return 'U';
    }
}
